package cn.weekdragon.gscore.adapter.filter.impl;

import cn.hutool.core.util.StrUtil;
import cn.weekdragon.gscore.adapter.filter.BotFilter;
import cn.weekdragon.gscore.adapter.model.EventExt;
import cn.weekdragon.gscore.adapter.model.message.Message;
import cn.weekdragon.gscore.adapter.model.message.Plain;

public class StripPrefixFilter implements BotFilter {

    private final String prefix;

    public StripPrefixFilter(String prefix) {
        this.prefix = prefix;
    }

    @Override
    public boolean doFilter(EventExt eventExt) {
        eventExt.setTempText(null);
        for (Message message : eventExt.getInMessage().toList()) {
            if(message instanceof Plain plain){
                String text = plain.getText();
                if(StrUtil.isEmpty(text)){
                    continue;
                }
                if(text.startsWith(prefix)){
                    plain.setText(text.substring(prefix.length()));
                }
                break;
            }
        }
        return false;
    }
}
